db = require '../utils/connect'
async = require 'async'
request = require 'request'
Article = require '../models/article'
moment = require 'moment'
log = require '../utils/log'
module.exports =  
	'get /article/:id': (req, res)->
		local = {category:'热门',back:true}
		if req.headers.referer?.indexOf '/c/'
			match = req.headers.referer.match /\/c\/(w+)/
			local.category = match[1] if match
		local.category = req.query.c if req.query?.c?
		async.series [
			(cb)->
				sql = "select articles_approved.*, article_detail.content from articles_approved join article_detail on articles_approved.id = article_detail.id and articles_approved.id = ?"
				db.queryOne sql,[req.params.id], (err, result)->
					if result
						local.article = result
						cb err
					else
						Article.complete req.params.id, cb
			(cb)->
				sql = "select accounts.* from accounts join articles_approved on accounts.nickname = articles_approved.nickname and articles_approved.id = ?"
				db.queryOne sql,[req.params.id], (err, result)->
					local.account = result
					cb err
			(cb)->
				cquery = ''
				params = []
				unless local.category is '热门'
					cquery = 'and accounts.category = ?' 
					params.push local.category
				sql = "select articles_approved.id,articles_approved.title,articles_approved.sourceurl from articles_approved join accounts on articles_approved.nickname = accounts.nickname #{cquery} order by id desc limit 10"
				db.runSql sql, [params], (err, result)->
					local.recomandist = result
					cb err
		],(err)->
			log.error err if err 
			res.render 'detail', local
		
	'get /': (req, res)->
		local =  {category:'热门'}
		async.series [
				(cb)->
					sql = "select id,title,digest,cover,nickname,datetime from articles_approved order by datetime desc limit 30"
					db.runSql sql, (err, result)->
						result?.forEach (item)->
							item.cover = Article.convertImg item.cover
							item.datetime = moment(item.datetime).format('YYYY-MM-DD')
						local.articleList = result
						cb err 
			],(err)->
				local.query = req.query
				log.error err if err 
				res.render 'index', local

	'get /c/:category':(req, res)->
		category = req.params.category
		local = {category,query:req.query}
		sql = "select articles_approved.* from articles_approved join accounts on accounts.category = ? and articles_approved.nickname = accounts.nickname order by datetime desc limit 30"
		db.runSql sql,[category], (err, result)->
			log.error err if err 
			result?.forEach (item)->
				item.cover = Article.convertImg item.cover
				item.datetime = moment(item.datetime).format('YYYY-MM-DD')
			local.articleList = result
			res.render 'index', local


	'get /more': (req, res)->
		page = req.query.p
		c = req.query.c
		local = {}
		if page>10
			return res.send 200
		where = ''
		params = [page*30]
		if c.indexOf('热门')<0
			where = ' join accounts on accounts.category = ? and articles_approved.nickname = accounts.nickname  '
			params.unshift c

		async.series [
				(cb)->
					sql = "select articles_approved.id,articles_approved.title,articles_approved.digest,articles_approved.cover,articles_approved.nickname,articles_approved.datetime from articles_approved #{where}   order by articles_approved.datetime desc limit ?,30"
					db.runSql sql, params , (err, result)->
						result?.forEach (item)->
							item.cover = Article.convertImg item.cover
							item.datetime = moment(item.datetime).format('YYYY-MM-DD')
						local.articleList = result
						cb err
			],(err)->
				res.send local

	'get /img': (req, res)->
		request.get(req.query.src).pipe res

	'get /mp/redirect':(req, res)->
		url = req.query.url
		res.redirect url